package com.sos.widget.util;

import android.graphics.Rect;
import android.os.Build;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

public class ButtonUtil {

    /**
     * 添加点击缩放效果
     *
     * @param scale    缩放比例  默认0.9f
     * @param duration 动画时长  默认150
     */
    public static void addClickScale(View view, Float scale, Integer duration) {
        if (view != null) {
            view.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        v.animate().scaleX(scale).scaleY(scale).setDuration(duration).start();
                    } else if (event.getAction() == MotionEvent.ACTION_UP) {
                        v.animate().scaleX(1f).scaleY(1f).setDuration(duration).start();
                        // 抬手的时候，回调OnClickListener点击事件
                        //v.performClick();
                    } else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
                        v.animate().scaleX(1f).scaleY(1f).setDuration(duration).start();
                    }
                    return false;
                }
            });
        }
    }


    /**
     * 设置点击放大效果。
     */
    public static void setClickZoomEffect(final View view, float scale) {
        if (view != null) {
            view.setOnTouchListener(new View.OnTouchListener() {
                boolean cancelled;
                Rect rect = new Rect();

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            scaleTo(v, scale);
                            break;
                        case MotionEvent.ACTION_MOVE:
                            if (rect.isEmpty()) {
                                v.getDrawingRect(rect);
                            }
                            if (!rect.contains((int) event.getX(), (int) event.getY())) {
                                scaleTo(v, 1);
                                cancelled = true;
                            }
                            break;
                        case MotionEvent.ACTION_UP:
                        case MotionEvent.ACTION_CANCEL: {
                            if (!cancelled) {
                                scaleTo(v, 1);
                            } else {
                                cancelled = false;
                            }
                            break;
                        }
                    }
                    return false;
                }
            });
        }
    }

    /**
     * 对view进行缩放。
     */
    public static void scaleTo(View v, float scale) {
        if (Build.VERSION.SDK_INT >= 11) {
            v.setScaleX(scale);
            v.setScaleY(scale);
        } else {
            float oldScale = 1;
            if (v.getTag(Integer.MIN_VALUE) != null) {
                oldScale = (Float) v.getTag(Integer.MIN_VALUE);
            }
            final ViewGroup.LayoutParams params = v.getLayoutParams();
            params.width = (int) ((params.width / oldScale) * scale);
            params.height = (int) ((params.height / oldScale) * scale);
            v.setTag(Integer.MIN_VALUE, scale);
        }
    }
}